package com.IMI.DatabaseActivity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DatabaseActivity extends Activity{
    /** Called when the activity is first created. */
	public DatabaseAdapter mDbHelper;
	private DatabaseAdapter mDbHelper_sort;
	/*private String[] inName;
    private String[] inDesc;
    private String[] inAdd;
    private int[] inPrice;
    private String[] allDesc;
    private String[] allAddress;
    private int[] allPrice;
    */
	private String[] sort_Name;
	private String[] sort_Id;
	private String[] sort_Price;
	private String[] allName;
	private String[] allId;
	private String[] allPrice;
	private String[] resName;
	private String[] resId;
	private String[] resPrice;
	private String[] Name;
	private String[] Price;
	private String[] Text;
    private int i;
    private Cursor cursor,s_cursor;
    private Intent intent;
    private Button SortItem;
    private Button SearchItem;
    private TextView SearchText;
    private String key;
    getData gd = new getData();
    private String[] dt =new String[100];
    private String[] tt = new String[100];
    private String[] pt = new String[100];
    private int num;
    private ArrayList<Data> datalist;
	private BaseAdapter dataAdapter;
	private ListView dataListView;
	private int count1;
	private static final int option1=1,option2=2;
	int flag=0;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gd.examineJSONFile(this.getApplicationContext());
        dt =gd.stock_name;
        tt =gd.stock_id;
        pt =gd.stock_price;
        num=gd.ctr;
        mDbHelper = new DatabaseAdapter(this);
        mDbHelper.open();
        
        
        for(int j=0;j<num;j++)
        {
        	System.out.println("dt["+j+"]is: "+dt[j]);
        	Log.d("Test before storing in database", tt[j]);
        	mDbHelper.createItem(dt[j],tt[j],pt[j]);
        }
        
        System.out.println("between");

		display();
		
		SortItem = (Button)findViewById(R.id.SortItem);
		SortItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sort();
			}
		});
		mDbHelper.close();
		
		SearchItem = (Button)findViewById(R.id.SearchItem);		
		SearchItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				search();
			}
		});
		
		dataListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				selectdialog(arg3);
			}
		});
        
	
    }
    
    private void search(){
			
			SearchText = (EditText)findViewById(R.id.SearchText);
			key = SearchText.getText().toString();
			System.out.println("value is " +key);
			flag=1;
			
			 mDbHelper.open();
			 Cursor cursor = mDbHelper.getItem(key);
			 startManagingCursor(cursor);
			 resName = new String[1000];
			 resId = new String[1000];
			 resPrice = new String[1000];
			 
			 if (cursor.moveToFirst()){
			   do{
				   System.out.println("inside");
				   resName[count1] = cursor.getString(cursor.getColumnIndex("stockname"));
				   resId[count1] = cursor.getString(cursor.getColumnIndex("stockid"));
				   resPrice[count1] = cursor.getString(cursor.getColumnIndex("stockprice"));
				   count1++;
			   
			   }while(cursor.moveToNext());
			   
			}
			
			mDbHelper.close();
			 
			dataListView = (ListView) findViewById(R.id.datalist);
	        
	        this.datalist = new ArrayList<Data>();
	        this.dataAdapter = new DataViewAdapter(this, datalist);

	        for(int i=0; i<count1; i++)
	        {
	        	String name= resName[i];
	        	String id1 = resId[i]; 
	        	String value = resPrice[i];
	        	Data team1 = new Data(name,id1,value);
	        	datalist.add(team1);
	        }
	        
	        dataListView.setAdapter(dataAdapter);
	        dataAdapter.notifyDataSetChanged();		
			
      }
    
    
    
    
    private void display(){
    	
    	
    	cursor = mDbHelper.getAllItems();
		startManagingCursor(cursor);
		flag=0;
    	allName = new String[1000];
		allId = new String[1000];
		allPrice = new String[100];
		int count=0;
		
		do{
		    /*allAddress[count]= cursor.getString(1);
		    allDesc[count]= cursor.getString(2);
		    allPrice[count]= cursor.getInt(3);
		    */
			allName[count]= cursor.getString(1);
			allId[count]= cursor.getString(2);
			allPrice[count]=cursor.getString(3);
			count++;
			
			
	
		}while(cursor.moveToNext());
		System.out.println("Display=" + count);
		
    	dataListView = (ListView) findViewById(R.id.datalist);
        
        this.datalist = new ArrayList<Data>();
        this.dataAdapter = new DataViewAdapter(this, datalist);

        for(int i=0; i<count; i++)
        {
        	//System.out.println("Name=" + allName[i]);
        	//System.out.println("Text=" + allId[i]);
        	
        	String Name= allName[i];
        	String text1 = allId[i];
        	Log.d("getting from array",text1);
        	String text2 = allPrice[i];
        	Data team1 = new Data(Name,text1,text2);
        	datalist.add(team1);
        	
        }
        
        dataListView.setAdapter(dataAdapter);
        dataAdapter.notifyDataSetChanged();
        
                   
    }
    
    
    
    
    private void sort(){
    	System.out.println("bottom1");
    	int count=0;
    	flag=2;
    	//mDbHelper_sort = new DatabaseAdapter(this);
        mDbHelper.open();
    	
    	s_cursor = mDbHelper.sortItem();
    	System.out.println("bottom2");
		startManagingCursor(s_cursor);
		System.out.println("bottom3");
		
    	sort_Name = new String[1000];
		sort_Id = new String[1000];
		sort_Id = new String[1000];
		
		do{
		    /*allAddress[count]= cursor.getString(1);
		    allDesc[count]= cursor.getString(2);
		    allPrice[count]= cursor.getInt(3);
		    */
			System.out.println("sortName=" + sort_Name[count]);
			sort_Name[count]= s_cursor.getString(1);
			sort_Id[count]= s_cursor.getString(2);
			sort_Price[count] = s_cursor.getString(3);
			count++;
		}while(s_cursor.moveToNext());
    	
		System.out.println("sort=" + count);
		
    	dataListView = (ListView) findViewById(R.id.datalist);
        
        this.datalist = new ArrayList<Data>();
        this.dataAdapter = new DataViewAdapter(this, datalist);

        for(int i=0; i<count; i++)
        {
        	System.out.println("Name=" + sort_Name[i]);
        	System.out.println("Text=" + sort_Id[i]);
        	String Name= sort_Name[i];
        	String text1 = sort_Id[i];
        	String text2 = sort_Price[i];
        	Data team1 = new Data(Name,text1,text2);
        	datalist.add(team1);
        	
        }
        
        dataListView.setAdapter(dataAdapter);
        dataAdapter.notifyDataSetChanged();

        mDbHelper.close();
        
    }
	
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        menu.add(0,option1,option1,"Preferences");
        menu.add(0,option2,option2,"Exit");
        return super.onCreateOptionsMenu(menu);
        }

        
        public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        	switch (item.getItemId()) {
            case option1:
            	Intent intent1 = new Intent(this,StockPreferences.class);
        		startActivityForResult(intent1, 0);
            	return true;
            	
            case option2:
            	return true;
            
           } 	
            	
        return false;
        }

        void selectdialog(long arg)
        {
        	Dialog dialog = new Dialog(this);

        	dialog.setContentView(R.layout.custom_dialog);
        	dialog.setTitle("Transaction");
        	TextView text1 = (TextView) dialog.findViewById(R.id.stocktext);
        	
        	if(flag==0)
        		text1.setText(allName[(int) arg]);
        	else if(flag==1)
        		text1.setText(resName[(int) arg]);
        	else
        		text1.setText(sort_Name[(int) arg]);
        	
        	Button buy = (Button) dialog.findViewById(R.id.button1);
        	Button sell = (Button) dialog.findViewById(R.id.button2);
        	dialog.show();
        	
        	buy.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Context mncontext = new Context()
					Intent intent2 = new Intent(getApplicationContext(), StockBuy.class);
		    		startActivityForResult(intent2, 0);
				}
			});
        	
        	sell.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Context mncontext = new Context()
					Intent intent3 = new Intent(getApplicationContext(), SellStock.class);
		    		startActivityForResult(intent3, 0);
				}
			});
        	
		}
}